# coding=utf-8
import os
import configparser
import tools.globalvar as globalvar
from jproperties import Properties


def i18n_frontdesk(lang,rootpath):
    '''
    获取供前端使用的翻译信息
    :param lang: 当前语言
    :param rootpath: 程序进入的根目录
    :return: 返回语言对应的翻译信息，字典格式{key:value}
    '''
    if lang.strip()=='':
        lang='zh_CN'
    i18nFilename=lang+'.properties'
    i18npath_frontdesk = rootpath+os.sep+"lang" + os.sep + "frontdesk"+os.sep+i18nFilename
    frontdesk_Dictionary = {}
    if os.path.exists(i18npath_frontdesk) == False:
        frontdesk_Dictionary={}
    else:
        # cf = configparser.ConfigParser()
        # cf.read(i18npath_frontdesk)
        # kvs = cf.items("info")
        # frontdesk_Dictionary=dict(kvs)
        p = Properties()
        props = p.load(open(i18npath_frontdesk.encode('utf-8')))
        _key = ""
        _value = ""
        for i in range(len(props.nodes)):
            item = props.nodes.__getitem__(i).__dict__
            for key, value in item.items():
                if key == 'key':
                    _key = value
                elif key == 'value':
                    _value = value
            frontdesk_Dictionary[_key] = _value
    return frontdesk_Dictionary


def i18n_backstage(lang,rootpath):
    '''
    获取供后台翻译使用的信息，同时设置全局翻译信息变量
    :param lang: 当前语言
    :param rootpath: 程序进入的根目录
    :return: 返回语言对应的翻译信息，字典格式{key:value}
    '''
    if lang.strip()=='':
        lang='zh_CN'
    i18nFilename=lang+'.properties'
    i18npath_backstage=rootpath+ os.sep +"lang" + os.sep + "backstage"+os.sep+i18nFilename
    frontdesk_Dictionary = {}
    if not os.path.exists(i18npath_backstage):
        frontdesk_Dictionary={}
    else:
        # cf = configparser.ConfigParser()
        # cf.read(i18npath_backstage)
        # kvs = cf.items("info")
        # frontdesk_Dictionary=dict(kvs)
        p = Properties()
        props = p.load(open(i18npath_backstage.encode('utf-8')))
        _key = ""
        _value = ""
        for i in range(len(props.nodes)):
            item = props.nodes.__getitem__(i).__dict__
            for key, value in item.items():
                if key == 'key':
                    _key = value
                elif key == 'value':
                    _value = value
            frontdesk_Dictionary[_key] = _value
        globalvar.set_i18nobject(frontdesk_Dictionary)
    return frontdesk_Dictionary

# if __name__ == '__main__':
#     p = Properties()
#     props=p.load(open(r"E:\\Work\\FCPOS--R3\\poscloud\\lang\\backstage\\foobar.properties".encode('utf-8')))
#         #print(f.read())
#     print(len(props.nodes))
#     rr=list(props.nodes)
#     _key=""
#     _value=""
#     for i in reversed(range(len(props.nodes))):
#         print(i)
#         item = props.nodes.__getitem__(i).__dict__
#         print(item)
#         for key,value in item.items():
#             if key=='key':
#                 _key =value
#             elif key=='value':
#                 _value=value
#         print(_key,_value)
